package com.suitsoft.ut.web.select;

import java.util.List;

import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.Page;

import com.suitsoft.ut.commons.Constants;
import com.suitsoft.ut.commons.LogAnnotation;
import com.suitsoft.ut.commons.LoginUser;
import com.suitsoft.ut.commons.UserHolder;
import com.suitsoft.ut.entity.base.Student;
import com.suitsoft.ut.entity.thesis.Thesis;
import com.suitsoft.ut.entity.thesis.ThesisFile;
import com.suitsoft.ut.service.base.StudentManager;
import com.suitsoft.ut.service.select.SelectManager;
import com.suitsoft.ut.web.CrudActionSupport;
import com.suitsoft.ut.web.InitListActionSupport;

/**
 * 选题
 *@author Suineg
 */
@Results({@Result(name=CrudActionSupport.RELOAD,location="selectt",type="redirect")})
public class SelecttAction extends InitListActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7169408763220933033L;

	/** 日志输出对象 */
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	
	/** 选题 服务层对象 */
	private SelectManager selectManager;
	/** 学生管理  服务层对象 */
	private StudentManager studentManager;
	
	
	/** 题的分页 */
	private Page<Thesis> page = new Page<Thesis>(Constants.PAGE_SIZE);
	/** 学院ID */
	private Long collegeId;
	/** 教师 */
	private String teacher;
	/** 团队 */
	private String group;
	/** 状态 */
	private Long stauts;
	/** 标识  1 - 评阅,2 - 团队,3 - 专家，4 - 基层 (专家没有评语。所以，专家审核时不会用到这个.)*/
	private Long  flag;
	/** 被选中ID */
	private List<Long> checkedIds;
	/** 题目 */
	private String titile;
	/** 教师名 */
	private String teacherName;
	/** 当前登陆用户 */
	private LoginUser loginUser;
	
	@Override
	public String execute() throws Exception {
		loginUser = UserHolder.getLoginUser();
		Student student = studentManager.findByUser(UserHolder.getLoginUser().getId());
		if(student == null){
			addActionMessage("请使用学生账号登陆！");
			return SUCCESS;
		}
		selectManager.findBySelectStudent(page, titile, teacherName, Constants.THESIS_FLAG_LITI, Constants.THSIS_STAUTS_LITI_FIRTHE_AUDIT,student);
		return SUCCESS;
	}
	@LogAnnotation(log = "选题 - 选题")
	public String select(){
		if(id == null || id == 0l){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		try {
			Student student = studentManager.findByUser(UserHolder.getLoginUser().getId());
			thesis = thesisManager.get(id);
			//判断题目是不是真实存在
			@SuppressWarnings("unused")
			Long r = thesis.getId() * 1;
			student.setThesis(thesis);
			thesis.getStudents().add(student);
			
			studentManager.save(student);
			
			thesisManager.save(thesis);
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败！");
			return RELOAD;
		}
		addActionMessage("操作成功！");
		return RELOAD;
	}
	@LogAnnotation(log = "选题 -取消选题")
	public String noSelect(){
		if(id == null || id == 0l){
			addActionMessage("非法操作！");
			return RELOAD;
		}
		try {
			thesis = thesisManager.get(id);
			//判断题目是不是真实存在
			@SuppressWarnings("unused")
			Long r = thesis.getId() * 1;
			
			Student student = studentManager.findByUser(UserHolder.getLoginUser().getId());
			if(student == null){
				addActionMessage("请用学生账户登陆！");
				return RELOAD;
			}
			thesis.getStudents().remove(student);
			
			thesisManager.save(thesis);
			
			student.setThesis(null);
			studentManager.save(student);
		} catch (Exception e) {
			e.printStackTrace();
			addActionMessage("操作失败！");
		}
		addActionMessage("操作成功！");
		return RELOAD;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Thesis getThesis() {
		return thesis;
	}
	public void setThesis(Thesis thesis) {
		this.thesis = thesis;
	}
	public ThesisFile getThesisFile() {
		return thesisFile;
	}
	public void setThesisFile(ThesisFile thesisFile) {
		this.thesisFile = thesisFile;
	}
	public Page<Thesis> getPage() {
		return page;
	}
	public void setPage(Page<Thesis> page) {
		this.page = page;
	}
	public Long getCollegeId() {
		return collegeId;
	}
	public void setCollegeId(Long collegeId) {
		this.collegeId = collegeId;
	}
	public String getTeacher() {
		return teacher;
	}
	public void setTeacher(String teacher) {
		this.teacher = teacher;
	}
	public String getGroup() {
		return group;
	}
	public void setGroup(String group) {
		this.group = group;
	}
	public Long getStauts() {
		return stauts;
	}
	public void setStauts(Long stauts) {
		this.stauts = stauts;
	}
	public Long getFlag() {
		return flag;
	}
	public void setFlag(Long flag) {
		this.flag = flag;
	}
	public List<Long> getCheckedIds() {
		return checkedIds;
	}
	public void setCheckedIds(List<Long> checkedIds) {
		this.checkedIds = checkedIds;
	}
	@Autowired
	public void setSelectManager(SelectManager selectManager) {
		this.selectManager = selectManager;
	}
	public String getTitile() {
		return titile;
	}
	public void setTitile(String titile) {
		this.titile = titile;
	}
	public String getTeacherName() {
		return teacherName;
	}
	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}
	public LoginUser getLoginUser() {
		return loginUser;
	}
	public void setLoginUser(LoginUser loginUser) {
		this.loginUser = loginUser;
	}
	@Autowired
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
	}
	
}

